import shortuuid

from .extensions import next_id


def seed():
    from vsogame.models.product import Product, ProductContent, ProductCategory, ProductMetadata, Metadata
    # migrator.create_model(Model)
    _id = shortuuid.uuid()

    if not ProductCategory.get_or_none(ProductCategory.screen_name == 'game'):
        ProductCategory.create(
            id=_id,
            screen_name='game'
        )

    from  vsogame.models.product import Metadata, ProductCategory
    game_category = ProductCategory.get(ProductCategory.screen_name == 'game')
    meta1 = Metadata()
    meta1.id = 'game-platform'
    meta1.screen_name = '激活平台'
    meta1.value_mode = 'select'
    meta1.extension = '[{"code":"PSN","screen_name":"PSN"},{"code":"Xbox","screen_name":"XBOX"}]'
    meta1.product_category = game_category
    meta2 = Metadata()
    meta2.id = 'game-language'
    meta2.screen_name = "支持语言"
    meta2.value_mode = 'muti-select'
    meta2.product_category = game_category
    meta2.extension = '[{"code":"zh_CN","screen_name":"中国大陆"},{"code":"us","screen_name":"美国"}]'
    meta3 = Metadata()
    meta3.id = 'game-area'
    meta3.screen_name = "有效区域"
    meta3.value_mode = 'muti-select'
    meta3.product_category = game_category
    meta3.extension = '[{"code":"zh","screen_name":"中国大陆"},{"code":"us","screen_name":"美国"},{"code":"eu","screen_name":"欧洲"}]'
    meta4 = Metadata()
    meta4.id = 'game-publish-date'
    meta4.screen_name = "发布日期"
    meta4.value_mode = 'text'
    meta4.product_category = game_category
    for m in [meta1, meta2, meta3, meta4]:
        if not Metadata.get_or_none(Metadata.id == m.id):
            m.save(force_insert=True)

    from vsogame.models.currency import Currency

    Currency.get_or_create(id='USD', defaults={'id': 'USD',
                                               'screen_name': 'United States Dollar',
                                               'type': 'main',
                                               'prefix': '$',
                                               'rate': 1
                                               })
    Currency.get_or_create(id='CNY', defaults={
        'id': 'CNY',
        'screen_name': 'CNY',
        'prefix': '￥',
        'rate': 6.63
    })

    from vsogame.models.setting import Setting

    # Setting.get_or_create(code='SECURITY_SERVICE_FEE', defaults={
    #     'code': 'SECURITY_SERVICE_FEE',
    #     'value': '1.98',
    #     'screen_name': '交易保障服务费',
    #     'desc': '交易保障服务费',
    #     'short_name': '交易保障'
    # })
    # Setting.get_or_create(code='DEFAULT_HEAD_IMAGE', defaults={
    #     'code': 'DEFAULT_HEAD_IMAGE',
    #     'value': 'http://oc3dkgv4l.bkt.clouddn.com/head_images/4.jpg',
    #     'screen_name': '默认头像地址',
    #     'desc': '默认头像地址'
    # })

    from  vsogame.models.product import Product, ProductContent, ProductCategory, ProductMetadata, Metadata
    Product.delete().execute()
    ProductContent.delete().execute()
    ProductMetadata.delete().execute()
    # migrator.create_model(Model)
    _id = shortuuid.uuid()
    import random
    category = ProductCategory.get(ProductCategory.screen_name == 'game')
    product = Product()
    product.id = _id
    product.title = '战锤:全面战争'
    product.sub_title = 'Buy Total War Steam CD Key'
    product.type = 'normal'
    product.cover = 'http://oc3dkgv4l.bkt.clouddn.com/KOKIA_00.png'
    product.like_count = random.randint(1, 2000)
    product.collection_count = random.randint(1, 3223)
    product.click_count = random.randint(1, 2302)
    product.category = category
    product.save(force_insert=True)
    # Product.create(
    #    **{
    #        'id': _id,
    #        'title': '战锤：全面战争',
    #        'sub_title': 'Buy Total War Steam CD Key',
    #        'type': 'normal',
    #        'category_id': category.id
    #    }
    # )
    ProductContent.create(
        **{
            'language': 'zh_CN',
            'product_id': _id,
            'summary': '《战锤：全面战争》是由The Creative Assembly开发的一款回合策略与即时战术相结合的大战略游戏，于2000年发行。2013年10月10日，经典战争策略游戏系列《全面战争》开发商Creative Assembly已经率先宣布全力支持Steam OS。',
            'detail': '该游戏主要讲述了在第二纪元时期玩家对抗莫拉格·巴尔入侵与三个观念分歧的阵营互相对抗的故事。'
        }
    )
    for m in Metadata.select().where(Metadata.product_category == category):
        if m.id == 'game-platform':
            ProductMetadata.create(
                **{
                    'product': product,
                    'metadata': m,
                    'value': '{"code":"xbox","value":"XBOX"}'
                }
            )
        elif m.id == 'game-area':
            ProductMetadata.create(
                **{
                    'product': product,
                    'metadata': m,
                    'value': '{"code":"eu","value":"欧洲"}'
                }
            )
        elif m.id == 'game-publish-date':
            ProductMetadata.create(
                **{
                    'product': product,
                    'metadata': m,
                    'value': '2015年10月1日'
                }
            )
        elif m.id == 'game-language':
            ProductMetadata.create(
                **{
                    'product': product,
                    'metadata': m,
                    'value': '{"code":"zh_CN","value":"简体中文"}'
                }
            )
    for p in ['魔兽世界',
              '权利的游戏',
              '上古卷轴5',
              '彩虹6号:围攻',
              '超级塞亚人',
              '吃怪兽，打豆豆',
              '传奇',
              '天下3',
              '守望先锋',
              '精灵宝可梦GO',
              '剑网3',
              '穿越火线',
              '梦幻西游',
              '地下城与勇士']:
        _id = shortuuid.uuid()
        product = Product()
        product.id = _id
        product.title = p
        product.sub_title = '{} 的副标题'.format(p)
        product.type = 'normal'
        product.click_count = random.randint(0, 1000)
        product.collection_count = random.randint(0, 500)
        product.category = category
        product.like_count = random.randint(0, 400)
        product.cover = 'http://oc3dkgv4l.bkt.clouddn.com/{}'.format(
            random.choice(['KOKIA_09.png', 'KOKIA_03.png', 'KOKIA_07.png']))
        product.save(force_insert=True)
        platform = (
            '{"code": "xbox", "value": "XBOX ONE"}',
            '{"code": "PSN", "value": "PSN"}',
            '{"code": "Stream","value": "Stream"}'
        )
        ProductContent.create(
            **{
                'language': 'zh-CN',
                'product_id': _id,
                'summary': '这是 {} 的简要描述'.format(p),
                'detail': '这是 {} 的详情'.format(p)
            }
        )
        metadata = Metadata.select()
        for m in metadata:
            if m.id == 'game-platform':
                ProductMetadata.create(
                    **{
                        'product': product,
                        'metadata': m,
                        'value': random.choice(platform)
                    }
                )
            elif m.id == 'game-area':
                ProductMetadata.create(
                    **{
                        'product': product,
                        'metadata': m,
                        'value': '{"code":"eu","value":"欧洲"}'
                    }
                )
            elif m.id == 'game-publish-date':
                ProductMetadata.create(
                    **{
                        'product': product,
                        'metadata': m,
                        'value': '2015年{}月{}日'.format(random.randint(1, 12), random.randint(1, 31))
                    }
                )
            elif m.id == 'game-language':
                ProductMetadata.create(
                    **{
                        'product': product,
                        'metadata': m,
                        'value': '{"code":"zh_CN","value":"简体中文"}'
                    }
                )

        from vsogame.models.member import Member

        Member.delete().execute()
        nick_names = ('苏浅沫△', 'ωǒ﹌嘸妸掫笩ヽ', '红尘殇雪', '姑娘你要像个爷们一样活着', 'KiSs℡尐壞蛋', '爱·宁缺毋滥', '雨樱﹋', '蔷の薇', '沵是硪嘚唯一', '沫小诺﹌')
        first_names = (
            '罗', '梁', '宋', '唐', '许', '韩', '冯', '邓', '曹', '彭', '曾', '肖', '田', '董', '袁', '潘', '于', '蒋', '蔡', '余', '杜',
            '叶',
            '程',
            '苏', '魏', '吕', '丁', '任', '沈', ' 姚', '卢', '姜', '崔', '钟', '谭', '陆', '汪', '范', '金', '石', '廖', '贾', '夏', '韦',
            '付',
            '方',
            '白', '邹', '孟', '熊', '秦', '邱', '江', '尹', '薛', '闫', '段', '雷', '侯', '龙', '史', '陶', '黎', '贺', '顾', ' 毛', '郝',
            '龚',
            '邵',
            '万', '钱', '严', '覃', '武', '戴', '莫', '孔', '向', '汤')
        last_name = (
            '璨', '琛', '璠', '琦', '锦', '琰', '玥', '琨', '玮', '璋', '璜', '珏', '琢', '珲', '璧', '宝', '玉', '金', '智', '睿', '知',
            '聪',
            '荣', '志', '立', '耀', '勋', '卓', '友', '建', '功', '彰', '达', '怀瑾', '璞初', '睿琳', '曾琪', '麒麟', '如龙', '鲲鹏', '玄武', '玳瑁',
            '家辉', '凯文', '博涵', '泰哲', '誉胜', '智宸', '一鸣', '杰豪', '志明', '学礼', '永胜', '恩泽', '有成', '安邦', '自光'
            , '俊娟', '小蓉', '春元', '湘平',
            '惠琼', '昭君', '一菲', '秀琼', '淑娇', '雪琪',
            ' 碧莲', '亚菲', '雅轩', '诗琪', '依玲', '依玲', '淑燕', '玉琼', '静涵', '娉婷',
            '文怡', '静萍', '韵涵', '亦菲', '秋洁', '慧云', '香凝', '苏梅', '佳君', '泽慧',
            '静君', '星辰', '子桐', '慧艳', '紫怡', '灵燕', '惠燕', '淑怡', '梓桐', '思璇',
            '雅红', '晓斐', '悦婷', '锦锦', '亚菲', '多多', '美静', '佳美', '雅淇', '晓',
        )
        from vsogame.models.country import Country
        Country.get_or_create(id='CN', defaults={
            'id': 'CN',
            'screen_name': '中国大陆',
            'logo': 'http://oc3dkgv4l.bkt.clouddn.com/countries/China.png'
        })
        Country.get_or_create(id='US', defaults={
            'id': 'US',
            'screen_name': 'U.S of America',
            'logo': 'http://oc3dkgv4l.bkt.clouddn.com/countries/United-States-of-America.png'
        })
        from  vsogame.models.member import Member, Account
        from  vsogame.models.auth import LocalAuth
        from  vsogame.models.currency import Currency
        country_list = Country.select()
        currency_list = Currency.select()
        import datetime

        index = random.randint(100000, 999999)

        Account.delete().execute()
        Member.delete().execute()
        LocalAuth.delete().execute()
        for i, n in enumerate(nick_names):
            index = index + 1
            account = Account()
            email = '{}@vsogame.com'.format(''.join(random.sample('zyxwvutsrqponmlkjihgfedcba', 6)))
            currency = random.choice(currency_list)

            account.id = '{}.{}'.format('1', str(index).zfill(24))
            account.currency = currency
            account.balance = 0.00
            account.frozen_amount = 0
            account.pending_amount = 0
            account.updated_at = datetime.datetime.now()
            account.created_at = datetime.datetime.now()
            account.save(force_insert=True)

            member = Member()
            member.id = index
            member.country = random.choice(country_list)
            member.buyer_rate = random.randint(60, 100)
            member.sales_rate = random.randint(60, 100)
            member.deal_count = random.randint(20, 100000)
            member.first_name = random.choice(first_names)
            member.last_name = random.choice(last_name)
            member.screen_name = n
            member.currency = currency
            member.grade = random.choice(('female', 'male'))
            member.head_image = 'http://oc3dkgv4l.bkt.clouddn.com/head_images/{}.jpg'.format(i + 1)
            member.updated_at = datetime.datetime.now()
            member.email = email
            member.account = account
            member.save(force_insert=True)

            auth = LocalAuth()

            auth.login_name = email

            auth.password = '12345678'
            auth.created_at = datetime.datetime.now()
            auth.member = member

            auth.save(force_insert=True)

        auth_list = list(LocalAuth.select())

        seller = random.choice(auth_list)

        seller.login_name = 'seller@vsogame.com'

        buyer = random.choice(auth_list)

        buyer.login_name = 'buyer@vsogame.com'

        seller.save()
        buyer.save()

        from vsogame.models.offer import Offer
        product_list = Product.select()
        member_list = Member.select()

        Offer.delete().execute()

        offer_index = random.randint(1000000, 9999999)
        index = 1
        from vsogame.models.inventory import Ticket
        for product in product_list:
            for i in range(1, 4):
                _id = offer_index + index

                count = random.randint(1, 40)
                sales_count = random.randint(1, 40)

                while True:
                    if sales_count > count:
                        sales_count = random.randint(1, 40)
                    else:
                        break
                offer = Offer.create(
                    product=product,
                    price=random.randint(1, 420),
                    count=random.randint(1, 40),
                    sales_count=random.randint(1, 20),
                    slug='CD-KEY',
                    id=_id,
                    member=random.choice(member_list)
                )
                for p in range(0, count - sales_count):
                    tikect = shortuuid.random()
                    Ticket.create(
                        offer=offer,
                        status='normal',
                        value=tikect
                    )
                index = index + 1

                # Setting.create(**{
                #     'code': 'SECURITY_SERVICE_FEE',
                #     'value': '1.98',
                #     'screen_name': '交易保障服务费',
                #     'desc': '交易保障服务费',
                #     'short_name': '交易保障'
                # })
                # Setting.create(**{
                #     'code': 'DEFAULT_HEAD_IMAGE',
                #     'value': 'http://oc3dkgv4l.bkt.clouddn.com/head_images/4.jpg',
                #     'screen_name': '默认头像地址',
                #     'desc': '默认头像地址'
                # })

        from .models.gateway import Gateway

        Gateway.delete().execute()

        currency = Currency.get(Currency.id == 'CNY')
        account = Account.create(
            currency=currency,
            id='2.{}'.format(str(next_id('ACCOUNT')).zfill(24))

        )
        Gateway.create(
            id='ALIPAY-000VOSGAME',
            currency=currency,
            account=account,
            screen_name='支付宝',
            fee_rate=1.40,
            logo='&#xe684;',

        )

        account = Account.create(
            currency=currency,
            id='2.{}'.format(str(next_id('ACCOUNT')).zfill(24))
        )

        Gateway.create(
            id='WECHAT-000VOSGAME',
            currency=currency,
            account=account,
            screen_name='微信支付',
            fee_rate=1.00,
            logo='&#xe685;'
        )

        currency = Currency.get_main_currency()

        account = Account.create(
            currency=currency,
            id='2.{}'.format(str(next_id('ACCOUNT')).zfill(24))
        )

        Gateway.create(
            id='PAYPAL-000VOSGAME',
            account=account,
            currency=currency,
            screen_name='Paypal (贝宝支付)',
            fee_rate=4.40,
            logo='&#xe683;'
        )

        from vsogame.models.service import Service
        Service.delete().execute()
        Service.create(id='TSS',
                       screen_name='交易保障服务',
                       short_name='交易保障',
                       fee=1.99)
        Service.create(id='TRS',
                       screen_name='交易推荐服务',
                       short_name='交易推荐',
                       fee=0.10,
                       mode='rate')
